<?php
 //============================================================================
 // ____________________________PLIGG TEAM ADDON_______________________________
 //
// This CLASS was created by AnAlienHolakres3 ||| XXXXX  XX     XX XXXX   XXXX
// (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
// @last_update                    20/02/2008 ||| XX     XX     XX X  XX  X  XX
// @version                              0.62 ||| XX     XXXXXX XX XXXXX  XXXXX
//
// This file can be used by Pligg community.
//=============================================================================

// Class TaskComment contains general information about task comments

class TaskComment {
 
    private $id =0;
    private $task_id=0; 
    private $team_id=0;
    private $content='';
    private $setup_date='';       
    private $creator=0;
    private $creator_name; 
    
    private $taskcomments_data=0;      
  
   private $read = true;
   //settings
   private $minimal_taskcomment_length=12; 
    

  
    
    //============================================================================
    //  Function read_taskcomments() gets taskcomments data (array) from database    
    //============================================================================      
    
    function read_taskcomments() {
        global $db;
        
      $id = $this->id;      
      
      $team_id = $this->team_id;       
                    
                         $taskcomment = $db->get_results("SELECT " . table_taskcomments . ".*,user_login FROM " . table_taskcomments . " 
                         JOIN " . table_users . " ON " . table_users.".user_id=" . table_taskcomments . ".taskcomment_creator_id WHERE 
                          taskcomment_team_id=$team_id ORDER BY taskcomment_date DESC");                                                  
                                      
          if ($taskcomment) {   
              $this->taskcomments_data = $taskcomment;
               return true;    
           }
              
        $this->read = false;
        return false;
    } 
    
    //============================================================================
      //  Function fill_print() assign team taskcomments smartys
      //============================================================================
      
      function fill_print()
      {
          global $main_smarty;
          
          $taskcomments_data = $this->taskcomments_data;;
          
          $team = new Team;
          $language = $team->get_language();                        
                        
          if ($taskcomments_data) {
                
                  foreach ($taskcomments_data as $comment) {
                      
                  
                  $taskcomments_id[] = $comment->taskcomment_id;
                  $taskcomments_task_id[] = $comment->taskcomment_task_id;                           
                  
                  $taskcomments_content[] = str_replace(array("\n", "\r"),"<br />",$comment->taskcomment_content);
                  $taskcomments_creator[] = $comment->taskcomment_creator_id;
                  $taskcomments_creator_name[] = $comment->user_login;
                                                  
                      
                  if ($language == "en"){
                   $taskcomments_date[] = date('Y/d/m h:i a',strtotime($comment->taskcomment_date));               
                                     
                  } else {                                       
                  $taskcomments_date[] = date('d.m.Y H:i',strtotime($comment->taskcomment_date));               
                  }                  
                }  
                
                
               
                $main_smarty->assign('team_taskcomments_id', $taskcomments_id);
                $main_smarty->assign('team_taskcomments_task_id', $taskcomments_task_id);
                $main_smarty->assign('team_taskcomments_content', $taskcomments_content);
                $main_smarty->assign('team_taskcomments_date', $taskcomments_date);
                $main_smarty->assign('team_taskcomments_creator', $taskcomments_creator);
                $main_smarty->assign('team_taskcomments_creator_name', $taskcomments_creator_name);
             }
             

         
      
       
      } 
    
    //============================================================================
    //  Function store() saves task comments in database
    //============================================================================
    
    function store() {
        global $db;
    
        if(!$this->setup_date) $this->setup_date=time();              
        
        $id                = $this->id;      
        $task_id           = $this->task_id;
        $team_id           = $this->team_id;
        $content           = $this->content;
        $setup_date        = $this->setup_date;
        $creator           = $this->creator;        
                     
        if($this->id===0) {   
           
           $sql = ("INSERT INTO " . table_taskcomments . " (taskcomment_content, taskcomment_date,
           taskcomment_task_id,taskcomment_team_id,taskcomment_creator_id) VALUES ('$content',  FROM_UNIXTIME($setup_date), 
           $task_id, $team_id, $creator)");        
       
       } 
        if($db->query($sql)){ 
              return true;     
              
        }     
          return false;
       }                                
    
    //============================================================================
    //  Function taskcomment_exist() finds out if there is some taskcomment in database
    //  @return true if taskcomment exist, otherwise false
    //  @param team_id
    //============================================================================      
    
    function taskcomment_exist($task_id,$team_id) {
        global $db;        
                        
        $exist=$db->get_var("SELECT taskcomment_id FROM " . table_taskcomments . " WHERE taskcomment_task_id= $task_id AND taskcomment_team_id=$team_id");

        if ($exist) { 
         return true; 
        }
        return false;
    }
    
  
    
      //==============================================================================
      // Function save_taskcomment_values()  sets values  and saves values to database           
      //==============================================================================  
      
      function save_taskcomment_values()
      {      global $current_user;
         
         $this->set_taskcomment_team_id($_REQUEST['id']);           
         $this->set_taskcomment_task_id($_POST['task_id']);                                                                            
         $this->set_taskcomment_creator_id($current_user->user_id);                     
         $this->set_taskcomment_content($_POST['taskcomment_content']);
         
          //store to database
          if (!$this->store()) { 
              //cannot save to dabase
              return false;
          }
          return true;
      }
      
    
 
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  CHECKING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     
      //==============================================================================
      //  Function get_taskcomments_data()  returns array of team tasks comment data
      //=============================================================================
      function get_taskcomments_data()
      {
          return $this->taskcomments_data;
      }
      
    //========================================================================
    // Function check_taskcomment_content_exist() checks if task name exists
    // @return true if task name exists otherwise false
    //========================================================================    

    function check_taskcomment_content_exist() {
        if (!$_POST["taskcomment_content"]) {            
            return false;
            }
            return true;            
    }     
    
    //========================================================================
    // Function check_task_name_length() checks task name length
    // @return true if name length is correct otherwise false
    //========================================================================    

    function check_taskcomment_content_length() {
       
       
            if (strlen(strip_tags(trim($_POST["taskcomment_content"]))) < $this->minimal_taskcomment_length) {
                return false;
                }

            if (strlen(strip_tags(trim($_POST["taskcomment_content"]))) > 450) {
                header ('Location: ./attack.php?id=16');
                die;
                }
            
            return true;
        
     }
       
     
    //============================================================================
    //  Function get_minimum_taskcomment_length() returns taskcomment length

    //============================================================================      
    function get_minimum_taskcomment_length() {
     return $this->minimal_taskcomment_length;
    }     

 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  SETTING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   
    //============================================================================
    //  Function set_taskcomment_id() sets taskcomment id
    //  @param task_id
    //============================================================================      
    function set_taskcomment_id($taskcomment_id) {
     $this->id = $taskcomment_id;
    } 
    
    //============================================================================
    //  Function set_taskcomment_team_id() sets taskcomment team id
    //  @param team_id
    //============================================================================      
    function set_taskcomment_team_id($team_id) {
     $this->team_id = $team_id;
    } 
    
    
    //============================================================================
    //  Function set_taskcomment_task_id() sets taskcomment task id
    //  @param task_id
    //============================================================================      
    function set_taskcomment_task_id($task_id) {
     $this->task_id= $task_id;
    }  
    
    //============================================================================
    //  Function set_taskcomment_creator_id() sets taskcomment creator id
    //  @param creator_id
    //============================================================================      
    function set_taskcomment_creator_id($creator_id) {
     $this->creator = $creator_id;
    }
    
    //============================================================================
    //  Function set_taskcomment_content() sets taskcomment content
    //  @param content
    //============================================================================      
    function set_taskcomment_content($content) {
        global $db;
     $this->content = htmlspecialchars($db->escape(strip_tags($content)));
    }  
    
  

     
    
}
?>

